//
//  CLTextViewCell.m
//  CareLink
//
//  Created by tanky on 16/8/19.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "CLTextViewCell.h"

@interface CLTextViewCell()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITextView *textView;


@end

@implementation CLTextViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.textView.delegate = self;
    self.textView.textColor = [UIColor grayColor];
    self.textView.contentInset = UIEdgeInsetsMake(15, 15, 15, 15);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.textString) {
            self.textView.text = self.textString;
            self.textView.textColor = [UIColor blackColor];
        }else{
            self.textView.text = self.placeHolderString;
        }
    });
    
}

- (void)scrollRangeToVisible:(NSRange)range{
    NSLog(@"scrollRangeToVisible");
}


//让textView 实现自动高度
- (void)textViewDidChange:(UITextView *)textView
{
    CGRect bounds = textView.bounds;
    
    // 计算 text view 的高度
    CGSize maxSize = CGSizeMake(bounds.size.width, CGFLOAT_MAX);
    CGSize newSize = [textView sizeThatFits:maxSize];
    bounds.size = newSize;
    
    textView.bounds = bounds;
    
    // 让 table view 重新计算高度
    UITableView *tableView = [self tableView];
    [tableView beginUpdates];
    [tableView endUpdates];
    self.symptomDesBlock(textView.text);
}

//让Cell 实现自动高度
- (UITableView *)tableView
{
    UIView *tableView = self.superview;
    
    while (![tableView isKindOfClass:[UITableView class]] && tableView) {
        tableView = tableView.superview;
    }
    
    return (UITableView *)tableView;
}


//以下是让TextView实现placeholder的功能
- (void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:self.placeHolderString]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length<1) {
        textView.text = self.placeHolderString;
        textView.textColor = [UIColor grayColor];
    }else{
    self.symptomDesBlock(textView.text);
    }
}



@end
